package com.shengzidong.keepacounts.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shengzidong.keepacounts.R;
import com.shengzidong.keepacounts.entity.Receipt;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/8/31.
 */
public class ReceiptAdapter extends RecyclerView.Adapter<ReceiptAdapter.MyViewHolder> {

    Context context;
    ArrayList<Receipt> receipts;
    TextView headerView;
    OnRecyclerViewItemClickListener mClickListener;
    OnRecyclerViewItemLongClickListener mLongClickListener;

    public static int TYPE_HEADER = 0;
    public static int TYPE_NORMAL = 1;

    public ReceiptAdapter(ArrayList<Receipt> receipts, Context context) {
        this.receipts = receipts;
        this.context = context;
        notifyItemInserted(0);
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        headerView = new TextView(context);
        headerView.setGravity(Gravity.CENTER);
        headerView.setWidth(parent.getWidth());
        headerView.setTextSize(30);
        headerView.setTextColor(Color.BLACK);

        float count = 0;
        for (int i = 0; i < receipts.size(); i++) {
            count += receipts.get(i).getMoney();
        }
        headerView.setText("今日:" + count);

        MyViewHolder holder;
        if (viewType == TYPE_HEADER) {
            return new MyViewHolder(headerView);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_receipt_list, parent, false);
            holder = new MyViewHolder(view);
        }
        return holder;

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            return TYPE_NORMAL;
        }

    }


    @Override
    public void onBindViewHolder(ReceiptAdapter.MyViewHolder holder, final int position) {

        if (getItemViewType(position) == TYPE_HEADER) return;
        holder.tvDate.setText(receipts.get(position).getDate());
        holder.tvMoney.setText(receipts.get(position).getMoney() + "");
        holder.tvCategory.setText(receipts.get(position).getCategory());
        holder.tvLocation.setText(receipts.get(position).getLocation());
        if (receipts.get(position).getLocation() == null) {
            holder.ivLocation.setVisibility(View.GONE);
        } else {
            holder.ivLocation.setVisibility(View.VISIBLE);
        }
        holder.tvLable.setText(receipts.get(position).getLabel());
        if (receipts.get(position).getLabel() == null) {
            holder.ivLable.setVisibility(View.GONE);
        } else {
            holder.ivLable.setVisibility(View.VISIBLE);
        }
        holder.tvMessage.setText(receipts.get(position).getMessage());
        if (receipts.get(position).getMessage() == null) {
            holder.ivMessage.setVisibility(View.GONE);
        } else {
            holder.ivMessage.setVisibility(View.VISIBLE);
        }
        Log.d("Adapter", "Adapter" + receipts.toString());

        holder.itemView.setTag(position);

        float count = 0;
        for (int i = 0; i < receipts.size(); i++) {
            count += receipts.get(i).getMoney();
        }
        headerView.setText("今日:" + count);

        if (position != 0) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickListener != null) {
                        mClickListener.onItemClick(view, position);
                    }
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    if (mLongClickListener != null) {
                        mLongClickListener.OnItemLongClick(view, position);
                    }
                    return true;
                }
            });
        }

    }

    public void setItemOnClickListener(OnRecyclerViewItemClickListener listener) {
        this.mClickListener = listener;
    }

    public void setItemOnLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mLongClickListener = listener;
    }


    @Override
    public int getItemCount() {
        return receipts.size();
    }

//    @Override
//    public void onClick(View view) {
////        MyDataBaseUtils db=new MyDataBaseUtils(context);
////        receipts=db.queryFromDataBase();
//        if (mClickListener != null) {
//            mClickListener.onItemClick(view, (int) view.getTag());
//        }
//    }
//
//    @Override
//    public boolean onLongClick(View view) {
//        if (mLongClickListener != null) {
//            mLongClickListener.OnItemLongClick(view, (int) view.getTag());
//        }
//        return true;
//    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvDate;
        TextView tvMoney;
        TextView tvCategory;
        TextView tvLocation;
        TextView tvLable;
        TextView tvMessage;

        ImageView ivLocation;
        ImageView ivLable;
        ImageView ivMessage;


        public MyViewHolder(View itemView) {
            super(itemView);
            tvDate = (TextView) itemView.findViewById(R.id.tvDate);
            tvMoney = (TextView) itemView.findViewById(R.id.tvMoney);
            tvCategory = (TextView) itemView.findViewById(R.id.tvCategory);
            tvLocation = (TextView) itemView.findViewById(R.id.tvLocation);
            tvLable = (TextView) itemView.findViewById(R.id.tvLable);
            tvMessage = (TextView) itemView.findViewById(R.id.tvMessage);

            ivLocation = (ImageView) itemView.findViewById(R.id.ivLocation);
            ivLable = (ImageView) itemView.findViewById(R.id.ivLable);
            ivMessage = (ImageView) itemView.findViewById(R.id.ivMessage);


        }
    }
}
